package words;

import checker.ArrayChecker;
import checker.SingleChecker;

import comparer.AWLComparer;
import comparer.NounComparer;
import comparer.NumberComparer;
import comparer.PrepComparer;
import comparer.PuntComparer;
import comparer.QuotationComparer;

public class Comparer {
	private Article origin_article;// the origin article
	private Article compared_article;// the article to be compared
	private Result result;
	private String error;
	private int id;
	public Comparer() {
		this.origin_article = null;
		this.compared_article = null;
		this.result = null;
	}

	public Boolean set_origin(Article article) {
		if (article != null) {
			this.origin_article = article;
			return true;
		} else {
			return false;
		}
	}

	public Boolean set_copy(Article article) {
		if (article != null) {
			this.compared_article = article;
			return true;
		} else {
			return false;
		}
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public Boolean execute() {
		try {
			comparsion();
			return true;
		} catch (Exception e) {
			error = e.toString();
			return false;
		}
	}

	public Result getResult() {
		return this.result;
	}

	public void comparsion() {
		try {
			QuotationComparer quot_comparer = new QuotationComparer();
			quot_comparer.set_origin(origin_article.getType(0));
			quot_comparer.set_compared(compared_article.getType(0));
			quot_comparer.compare();
			AWLComparer awl_comparer = new AWLComparer();
			awl_comparer.set_origin(origin_article.getType(1));
			awl_comparer.set_compared(compared_article.getType(1));
			awl_comparer.compare();

			NounComparer noun_comparer = new NounComparer();
			noun_comparer.set_origin((Noun)origin_article.getType(2));
			noun_comparer.set_compared((Noun)compared_article.getType(2));
			noun_comparer.compare();

			PuntComparer punt_comparer = new PuntComparer();
			punt_comparer.set_origin((Punctuation)origin_article.getType(3));
			punt_comparer.set_compared((Punctuation)compared_article.getType(3));
			punt_comparer.compare();
			
			NumberComparer numb_comparer = new NumberComparer();
			numb_comparer.set_origin((NumberStat)origin_article.getType(4));
			numb_comparer.set_compared((NumberStat)compared_article.getType(4));
			numb_comparer.compare();
			
			PrepComparer prep_comparer = new PrepComparer();
			prep_comparer.set_origin((Preposition)origin_article.getType(5));
			prep_comparer.set_compared((Preposition)compared_article.getType(5));
			prep_comparer.compare();
			
			int noun_num = noun_comparer.getResult();
			int quot_num = quot_comparer.getResult();
			int awl_num = awl_comparer.getResult();
			int punt_num = punt_comparer.getResult();
			int numb_num = numb_comparer.getResult();
			int prep_num = prep_comparer.getResult();
			Result result = new Result(awl_num,noun_num,prep_num,punt_num,quot_num,numb_num);
			result.setId(id);

			this.result = result;
		} catch (Exception e) {
			e.printStackTrace();
			this.result = null;
		}

	}

	public void show_error() {
		if (this.error != null) {
			System.out.println(this.error);
		} else {
			System.out.println("");
		}
	}
}
